package com.starblink.android.basic.widget

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import com.starblink.android.basic.R
import com.starblink.android.basic.databinding.ViewBackIconBinding
import com.starblink.android.basic.graphic.SimpleColorFilter

/**
 * 返回按钮View
 */
class BackIconView @JvmOverloads constructor(
    context: Context,
    attrSet: AttributeSet?,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrSet, defStyleAttr) {

    private val binding: ViewBackIconBinding

    var iconColor: Int = Color.TRANSPARENT
        set(value) {
            binding.icon.colorFilter = SimpleColorFilter(value)
        }

    init {
        binding = ViewBackIconBinding.inflate(LayoutInflater.from(context), this, true)
        val typedArray = context.obtainStyledAttributes(attrSet, R.styleable.BackIconView)
        val iconColor = typedArray.getColor(
            R.styleable.BackIconView_iconColor,
            ContextCompat.getColor(context, com.starblink.basic.style.R.color.sk_gray800)
        )
        typedArray.recycle()
        this.iconColor = iconColor
        binding.icon.colorFilter = SimpleColorFilter(iconColor)
        setOnClickListener {
            if (context is Activity) {
                context.setResult(Activity.RESULT_OK, Intent().putExtra("isLogin", false))
                context.finish()
            }
        }
    }


    override fun onFinishInflate() {
        super.onFinishInflate()
    }

}